from django.urls import path, include, re_path
from rest_framework import routers
from rest_framework.authtoken.views import obtain_auth_token
from . import views
from .views import (
    SystemMetricsView, WebSSHView, NativeSSHView, SystemExecuteView, SimpleFileView,
    SSHSessionListView, SSHSessionReplayView, SSHCommandListView,
    SSHAccessControlView, SSHTokenValidationView, RoleViewSet,
    GroupRoleViewSet, RoleMenuPermissionViewSet, MenuPermissionManagementView,
    MenuPermissionViewSet, get_user_permissions, DynamicMenuViewSet, HostsAndGroupsView
)
from .api.audit_views import SSHSessionViewSet, SSHCommandViewSet, AuditRuleViewSet, AuditAlertViewSet

router = routers.DefaultRouter()
router.register(r'hosts', views.HostViewSet)
router.register(r'hostgroups', views.HostGroupViewSet)
router.register(r'hosttypes', views.HostTypeViewSet)
router.register(r'connections', views.ConnectionRecordViewSet)
router.register(r'commands', views.CommandLogViewSet)
router.register(r'users', views.UserViewSet)
router.register(r'usergroups', views.UserGroupViewSet)
router.register(r'userroles', views.UserRoleViewSet)
router.register(r'usergrouproles', views.UserGroupRoleViewSet)
router.register(r'alerts', views.AlertViewSet)
router.register(r'menus', views.MenuItemViewSet)
router.register(r'menupermissions', views.GroupMenuPermissionViewSet)
router.register(r'groups', views.GroupViewSet)

# SSH审计相关路由
router.register(r'audit/sessions', SSHSessionViewSet, basename='ssh-sessions')
router.register(r'audit/commands', SSHCommandViewSet, basename='ssh-commands')
router.register(r'audit/rules', AuditRuleViewSet, basename='audit-rules')
router.register(r'audit/alerts', AuditAlertViewSet, basename='audit-alerts')
# 注释掉冲突的回放路由，使用自定义路由
# router.register(r'audit/replay', SSHReplayViewSet, basename='ssh-replay')

# 角色权限系统路由
router.register(r'roles', RoleViewSet, basename='roles')
router.register(r'group-roles', GroupRoleViewSet, basename='group-roles')
router.register(r'role-menu-permissions', RoleMenuPermissionViewSet, basename='role-menu-permissions')
router.register(r'menu-permissions', MenuPermissionViewSet, basename='menu-permissions')

# 动态菜单管理路由
router.register(r'dynamic-menus', DynamicMenuViewSet, basename='dynamic-menus')

urlpatterns = [
    path('', include(router.urls)),
    path('api-token-auth/', obtain_auth_token),
    path('system-metrics/', SystemMetricsView.as_view(), name='system-metrics'),
    re_path(r'^webssh/(?P<host_id>\d+)/$', WebSSHView.as_view(), name='webssh'),
    re_path(r'^native-ssh/(?P<host_id>\d+)/$', NativeSSHView.as_view(), name='native-ssh'),
    # Ansible相关路由已迁移到ansible_management应用
    path('system/execute/', SystemExecuteView.as_view(), name='system-execute'),
    path('file/', SimpleFileView.as_view(), name='simple-file'),

    # 新的审计API路由
    path('audit/sessions/', SSHSessionListView.as_view(), name='audit-sessions-list'),
    path('audit/sessions/<str:session_id>/replay/', SSHSessionReplayView.as_view(), name='audit-session-replay'),
    path('audit/commands/', SSHCommandListView.as_view(), name='audit-commands-list'),

    # 简化的命令API
    path('audit/session-commands/', views.SessionCommandsView.as_view(), name='session-commands'),

    # SSH访问控制API
    path('ssh/request-access/', SSHAccessControlView.as_view(), name='ssh-request-access'),
    path('ssh/validate-token/', SSHTokenValidationView.as_view(), name='ssh-validate-token'),

    # 菜单权限管理API
    path('menu-permissions/tree/', MenuPermissionManagementView.as_view(), {'action': 'get_menu_tree'}, name='menu-permission-tree'),
    path('menu-permissions/groups/', MenuPermissionManagementView.as_view(), {'action': 'get_groups_with_roles'}, name='groups-with-roles'),
    path('menu-permissions/check/', MenuPermissionManagementView.as_view(), {'action': 'check_user_permission'}, name='check-user-permission'),

    # 新权限系统API
    path('auth/permissions/', get_user_permissions, name='user-permissions'),

    # 主机和主机组智能提示API
    path('inventory/hosts-and-groups/', views.HostsAndGroupsView.as_view(), name='hosts-and-groups'),
]
